package it.maggioni.struts2.interceptors;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class TimingInterceptor implements Interceptor {

	private static final long serialVersionUID = 8547436719646415486L;

	@Override
	public void destroy() {
		System.out.println("TimingInterceptor.destroy()");
	}

	@Override
	public void init() {
		System.out.println("TimingInterceptor.init()");
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String className = invocation.getAction().getClass().getName();
		long startTime = System.currentTimeMillis();
		System.out.println("Timer for " + className + " started");
		String result = invocation.invoke();
		long endTime = System.currentTimeMillis();
		System.out.println("Timer for " + className + " stopped; execution time: " + (endTime - startTime) + " ms");
		return result;
	}
	

}
